/* Copyright (c) 2009, Jeff Kulas
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *  * Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * The names of its contributors 
 *    may not be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *    
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

/* This code is copied from the jna project: /trunk/jnalib/contrib/w32keyhook/KeyHook.java
 */

package fwm;

import com.sun.jna.examples.win32.*;
import com.sun.jna.examples.win32.User32.LowLevelKeyboardProc;
import com.sun.jna.examples.win32.User32.KBDLLHOOKSTRUCT;
import com.sun.jna.examples.win32.W32API.LRESULT;
import com.sun.jna.examples.win32.W32API.WPARAM;
import com.sun.jna.examples.win32.W32API.HMODULE;
import com.sun.jna.examples.win32.User32.HHOOK;

public class KeyBlock {
    private static HHOOK hhk;
    private static LowLevelKeyboardProc keyboardHook;
    private static LRESULT stopLRESULT;
    private static User32 lib;
    
    public static void startBlock() {
    	
    	//try windows first
    	try {
    		lib = User32.INSTANCE;
    		HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    		stopLRESULT = new LRESULT(1);
    		keyboardHook = new LowLevelKeyboardProc() {
    			public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
    				if (nCode >= 0) {
    					switch(wParam.intValue()) {
    					case User32.WM_KEYUP:
    					case User32.WM_KEYDOWN:
    					case User32.WM_SYSKEYUP:
    					case User32.WM_SYSKEYDOWN:
    					}
    					switch(info.vkCode) {
    					case 91: //left windows key
    					case 92: //right windows key
    					case 115: //f4
    					case 9: //tab
    					case 160: //shift
    					case 161: //shift
    					//case 93: //menu key
    						return stopLRESULT;
    					}

    				}
    				//System.out.println("*** key=" + info.vkCode);
    				return lib.CallNextHookEx(hhk, nCode, wParam, info.getPointer());
    			}
    		};
    		hhk = lib.SetWindowsHookEx(User32.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    	} catch (Exception e) {}
    }
    
    public static void unHook() {
    	//try windows first
    	try {
    		lib.UnhookWindowsHookEx(hhk);
    	} catch (Exception e) {}    	
    }
}
